﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class ScheduleTemplateEditor : ChildWindow
    {
        public DateTime startDay;
        private bool initialized = false;

        public ScheduleTemplateEditor()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (cmbScheduleTemplateName.Items.Count > 0)
            {
                //saveCurrentTemplate();
                await sc0.save();
                await sc1.save();
                await sc2.save();
                await sc3.save();
                await sc4.save();
                await sc5.save();
                await sc6.save();
            }
            Globals.schedule.refreshData(true);
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Globals.schedule.refreshData(true);
            this.DialogResult = false;
        }

        private async void initSchedules()
        {
            if (initialized)
            {
                //saveCurrentTemplate();
                await sc0.save();
                await sc1.save();
                await sc2.save();
                await sc3.save();
                await sc4.save();
                await sc5.save();
                await sc6.save();
            }
            await Globals.db.GetScheduleObjects("ScheduleTemplateID>0");
            sc0.initScheduleTemplate(Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex].col_ID, startDay);
            sc1.initScheduleTemplate(Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex].col_ID, startDay.AddDays(1));
            sc2.initScheduleTemplate(Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex].col_ID, startDay.AddDays(2));
            sc3.initScheduleTemplate(Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex].col_ID, startDay.AddDays(3));
            sc4.initScheduleTemplate(Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex].col_ID, startDay.AddDays(4));
            sc5.initScheduleTemplate(Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex].col_ID, startDay.AddDays(5));
            sc6.initScheduleTemplate(Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex].col_ID, startDay.AddDays(6));
        }

        public async void init(DateTime selectedDate)
        {
            startDay = selectedDate;
            initialized = false;
            // Load template names
            await Globals.db.GetScheduleTemplateObjects("1=1 order by Name");
            // Populate the combo-box
            cmbScheduleTemplateName.Items.Clear();
            foreach (ServiceReferenceOpenRPOS.ScheduleTemplate st in Globals.db.ScheduleTemplateRecords)
            {
                cmbScheduleTemplateName.Items.Add(st.col_Name);
            }
            // Load schedule for this template
            if (cmbScheduleTemplateName.Items.Count > 0)
            {
                cmbScheduleTemplateName.SelectedIndex = 0;
                // Initialize each schedule control
                initSchedules();
                btnDelete.IsEnabled = true;
            }
            else
            {
                btnDelete.IsEnabled = false;
                sc0.clearScheduleTemplateDisplay();
                sc1.clearScheduleTemplateDisplay();
                sc2.clearScheduleTemplateDisplay();
                sc3.clearScheduleTemplateDisplay();
                sc4.clearScheduleTemplateDisplay();
                sc5.clearScheduleTemplateDisplay();
                sc6.clearScheduleTemplateDisplay();
            }
            initialized = true;
        }

        private void cmbScheduleTemplateName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbScheduleTemplateName.Items.Count > 0)
                initSchedules();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // Add a new template
            AddScheduleTemplateDialog astd = new AddScheduleTemplateDialog();
            astd.ste = this;
            astd.Show();
        }

        private async void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            // Delete the schedule template
            if (cmbScheduleTemplateName.Items.Count > 0)
            {
                // Ensure that the user really wants to delete the selected template
                if (MessageBox.Show("Are you sure you want to delete the selected schedule template?", "Confirm", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    // Delete schedule template records...
                    await Globals.db.GetScheduleObjects("ScheduleTemplateID=" + Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex].col_ID);
                    foreach (ServiceReferenceOpenRPOS.Schedule sch in Globals.db.ScheduleRecords)
                    {
                        await Globals.db.DeleteScheduleObject(sch);
                    }
                    // Delete the template
                    await Globals.db.DeleteScheduleTemplateObject(Globals.db.ScheduleTemplateRecords[cmbScheduleTemplateName.SelectedIndex]);
                    // Re-load
                    init(startDay);
                }
            }
        }

    }
}

